package com.puzzud.engine.input;

import java.util.Vector;

// InputDevice
public class InputDevice extends InputObject
{
    protected Vector<InputButton> buttonBuffer;
    protected int buttonBufferSizeLimit;

    // TODO Make this class protected?
    public class InputButton
    {
        public static final int STATE_UP			= -1;
        public static final int STATE_RELEASED			= 0;
        public static final int STATE_PRESSED			= 1;
        public static final int STATE_DOWN			= 2;

        protected int id;
        protected int state;

        public InputButton( int id, int state )
        {
            this.id = id;
            this.state = state;
        }

        public void setId( int id )
        {
            this.id = id;
        }

        public int getId()
        {
            return( id );
        }

        public void setState( int state )
        {
            this.state = state;
        }

        public int getState()
        {
            return( state );
        }

        public void ageState()
        {
            if( getState() == STATE_PRESSED )
            {
                setState( STATE_DOWN );
            }
            else
            if( getState() == STATE_RELEASED )
            {
                setState( STATE_UP );
                buttonBuffer.remove( this );
            }
            else
            if( getState() == STATE_UP )
            {
                buttonBuffer.remove( this );
            }
        }
    }

    public InputDevice()
    {
        buttonBuffer = new Vector<InputButton>();

        setBufferSize( 1 );
    }

    public void read()
    {
        int index;
        InputButton tempButton;
        int id;

        for( index = 0; index < buttonBuffer.size(); index++ )
        {
            tempButton = ( InputButton )buttonBuffer.elementAt( index );

            if( tempButton.state == InputButton.STATE_RELEASED )
            {

                tempButton = buttonBuffer.remove( index );
                //tempButton.state = InputButton.BUTTON_STATE_UP;
                //tempButton = new InputButton( tempButton.id, InputButton.BUTTON_STATE_UP );
                //buttonBuffer.add( tempButton );
            }
            else
            if( tempButton.state == InputButton.STATE_PRESSED )
            {
                tempButton = buttonBuffer.remove( index );
                tempButton.state = InputButton.STATE_DOWN;
                //tempButton = new InputButton( tempButton.id, InputButton.BUTTON_STATE_DOWN );
                buttonBuffer.add( tempButton );
            }
        }
    }

    public void setBufferSize( int size )
    {
        buttonBuffer.clear();
        buttonBufferSizeLimit = size;
    }

    public int getButtonState( int b )
    {
        int index;
        InputButton tempButton;

        for( index = 0; index < buttonBuffer.size(); index++ )
        {
            tempButton = ( InputButton )buttonBuffer.get( index );

            if( tempButton.id == b )
            {
                return( tempButton.state );
            }
        }

        return( InputButton.STATE_UP );
    }

    public boolean checkButton( int b )
    {
        if( ( getButtonState( b ) == InputButton.STATE_PRESSED ) ||
            ( getButtonState( b ) == InputButton.STATE_DOWN ) )
        {
            return( true );
        }
        else
        {
            return( false );
        }
    }

    public void pressButton( int b )
    {
        int index;
        InputButton tempButton;

        for( index = 0; index < buttonBuffer.size(); index++ )
        {
            tempButton = ( InputButton )buttonBuffer.get( index );

            if( tempButton.id == b )
            {
                System.out.println( "pressButton:" + tempButton.getState() );
                buttonBuffer.remove( index );
            }
            else
            {
                tempButton.ageState();
            }
        }

        tempButton = new InputButton( b, InputButton.STATE_PRESSED );
        buttonBuffer.add( tempButton );
    }

    public void releaseButton( int b )
    {
        int index;
        InputButton tempButton;

        boolean badRelease = false;

        for( index = 0; index < buttonBuffer.size(); index++ )
        {
            tempButton = ( InputButton )buttonBuffer.get( index );

            if( tempButton.id == b )
            {
                // DEBUG:
                //System.out.println( "releaseButton:" + tempButton.id + ":" + tempButton.getState() );
                
                if( tempButton.getState() == InputButton.STATE_DOWN )
                {
                    badRelease = true;
                }
                
                buttonBuffer.remove( index );
            }
            else
            {
                tempButton.ageState();
            }
        }

        if( badRelease == false )
        {
            tempButton = new InputButton( b, InputButton.STATE_RELEASED );
            buttonBuffer.add( tempButton );
        }

        // DEBUG:
        //System.out.println( buttonBuffer.size() );
    }
}
